package com.lin.pay.wx.utils;

import com.lin.common.utils.BeanCopyUtils;
import com.lin.pay.wx.WXPayConstant;
import com.lin.pay.wx.sdk.WXPayConstants;
import com.lin.pay.wx.sdk.WXPayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

//
public class WxMsgUtils {
    private static final Logger logger = LoggerFactory.getLogger(WxMsgUtils.class);

    /**
     * 参数需要重新进行签名计算，参与签名的参数为：appId、timeStamp、nonceStr、package、signType，参数区分大小写。
     *
     * @param reqData
     * @return
     * @throws Exception
     */
    public static Map<String, String> buildWCPayRequest(Map<String, String> reqData, String key) throws Exception {
        Map<String, String> req = new HashMap<>();
        req.put(WXPayConstant.APPID_HUMP, reqData.get(WXPayConstant.APPID));
        req.put(WXPayConstant.TIME_STAMP, String.valueOf(System.currentTimeMillis()/1000));
        req.put(WXPayConstant.NONCE_STR, BeanCopyUtils.getUUID32());//reqData.get(WXPayConstant.NONCE_STR_UNDERLINE))
        req.put(WXPayConstant.PACKAGE, WXPayConstant.PREPAY_ID +"="+ reqData.get(WXPayConstant.PREPAY_ID));
        req.put(WXPayConstant.SIGN_TYPE, WXPayConstants.HMACSHA256);
        logger.info("map to xml is\n:{}",WXPayUtil.mapToXml(req));
        req.put(WXPayConstant.SIGN, WXPayUtil.generateSignature(req, key, WXPayConstants.SignType.HMACSHA256));
        return req;
    }

}
